package cn.sdormitory.job;

import cn.sdormitory.basedata.dao.BClassDao;
import cn.sdormitory.basedata.dao.BDormitoryDao;
import cn.sdormitory.basedata.dao.BStudentDao;
import cn.sdormitory.basedata.entity.BClass;
import cn.sdormitory.basedata.entity.BDormitory;
import cn.sdormitory.basedata.entity.BStudent;
import cn.sdormitory.common.sms.TxSmsTemplate;
import cn.sdormitory.school.dao.SchoolPatrolDao;
import cn.sdormitory.school.dao.SchoolPatrolHandleDao;
import cn.sdormitory.school.entity.SchoolPatrolHandle;
import cn.sdormitory.school.vo.SchoolPatrolVo;
import cn.sdormitory.sys.dao.SysDictDetailDao;
import cn.sdormitory.sys.dao.SysUserDao;
import cn.sdormitory.sys.entity.SysDictDetail;
import cn.sdormitory.sys.entity.SysUser;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

//@Component
public class SmsScheduled {

    @Autowired
    SchoolPatrolDao schoolPatrolDao;

    @Autowired
    SchoolPatrolHandleDao schoolPatrolHandleDao;

    @Autowired
    SysUserDao sysUserDao;

    @Autowired
    BClassDao bClassDao;

    @Autowired
    private BStudentDao bStudentDao;

    @Autowired
    private BDormitoryDao bDormitoryDao;

    @Autowired
    SysDictDetailDao sysDictDetailDao;

    @Autowired
    private TxSmsTemplate txSmsTemplate;

    /**
     * 检查发送失败的短信重新发送
     */
    @Scheduled(cron = "0 0 0/1 * * ?")
    public void dutyFailedSmsRePush(){
        List<SchoolPatrolHandle> dutyHandleSmsFailed = schoolPatrolHandleDao.findDutyFailedSms();
        for(SchoolPatrolHandle item : dutyHandleSmsFailed){
            SysUser sysUser = sysUserDao.selectById(item.getChargeTeacherId());
            String[] params = {item.getSmsParams()};
            String result = txSmsTemplate.sendMesModel(sysUser.getPhone(), params, 1);
            if("OK".equals(result)){
                item.setSmsStatus("0");
                schoolPatrolHandleDao.updateById(item);
            }
        }
    }

    /**
     * 值班短信二次推送
     */
//    @Scheduled(cron = "0 15 8 * * ?")
//    @Scheduled(cron = "0 0/2 * * * ?")
//    public void dutySmsSecondPush(){
//        //查询昨天未处理数据，为空则不处理
//        List<SchoolPatrolHandle> noHandleYesterdayData = schoolPatrolHandleDao.findNoHandleYesterdayData();
//        if(noHandleYesterdayData == null && noHandleYesterdayData.size() <= 0){
//            return;
//        }
//
//        Calendar cal = Calendar.getInstance();
//        cal.add(Calendar.DATE,-1);
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd号");
//
//        for(SchoolPatrolHandle item : noHandleYesterdayData){
//            StringBuilder smsContent = new StringBuilder();
//            SchoolPatrolVo schoolPatrol = schoolPatrolDao.findById(item.getPatrolId());
//            if(!("1".equals(schoolPatrol.getPatrolType()))){
//                String classId = schoolPatrol.getClassId();
//                BClass bClass = bClassDao.selectById(classId);
//                QueryWrapper<SysDictDetail> wrapper = new QueryWrapper<>();
//                wrapper.eq("dict_type","class_dept").eq("dict_value",bClass.getClassDept());
//                SysDictDetail sysDictDetail = sysDictDetailDao.selectOne(wrapper);
//                String classDept = sysDictDetail.getDictLabel();
//
//                smsContent.append(bClass.getClassName());
//                if(!StringUtils.isEmpty(schoolPatrol.getStudentNo())){
//                    QueryWrapper<BStudent> stuWrapper = new QueryWrapper<>();
//                    stuWrapper.eq("student_no",schoolPatrol.getStudentNo());
//                    BStudent bStudent = bStudentDao.selectOne(stuWrapper);
//                    smsContent.append("-").append(bStudent.getStudentName());
//                }
//                String[] params = {smsContent.toString(),sdf.format(cal.getTime())};
//
//                if("启蒙星".equals(classDept)){
//                    QueryWrapper<SysUser> userWrapper = new QueryWrapper<>();
//                    userWrapper.eq("username","李琴辉");
//                    SysUser sysUser = sysUserDao.selectOne(userWrapper);
//                    if(sysUser == null){
//                        return;
//                    }
//                    txSmsTemplate.sendMesModel(sysUser.getPhone(),params,2);
//                }else if("BCSP".equals(classDept)){
//                    QueryWrapper<SysUser> userWrapper = new QueryWrapper<>();
//                    userWrapper.eq("username","汤金霞");
//                    SysUser sysUser = sysUserDao.selectOne(userWrapper);
//                    if(sysUser == null){
//                        return;
//                    }
//                    txSmsTemplate.sendMesModel(sysUser.getPhone(),params,2);
//                }else if("实训部".equals(classDept)){
//                    QueryWrapper<SysUser> userWrapper = new QueryWrapper<>();
//                    userWrapper.eq("username","黄群英");
//                    SysUser sysUser = sysUserDao.selectOne(userWrapper);
//                    if(sysUser == null){
//                        return;
//                    }
//                    txSmsTemplate.sendMesModel(sysUser.getPhone(),params,2);
//                }
//                //宿舍
//            }else {
//                BDormitory bDormitory = bDormitoryDao.selectById(schoolPatrol.getDormitoryId());
//                QueryWrapper<SysDictDetail> sysDictDetailWrapper = new QueryWrapper<>();
//                sysDictDetailWrapper.eq("dict_type","sys_building")
//                        .eq("dict_value",bDormitory.getBuildingNo());
//                SysDictDetail sysDictDetail = sysDictDetailDao.selectOne(sysDictDetailWrapper);
//
//                smsContent.append(sysDictDetail.getDictLabel())
//                        .append("-")
//                        .append(bDormitory.getDormitoryNo());
//                String[] params = {smsContent.toString(),sdf.format(cal.getTime())};
//
//                QueryWrapper<SysUser> userWrapper = new QueryWrapper<>();
//                userWrapper.eq("username","守伟");
//                SysUser sysUser = sysUserDao.selectOne(userWrapper);
//                if(sysUser == null){
//                    return;
//                }
//                txSmsTemplate.sendMesModel(sysUser.getPhone(),params,2);
//            }
//        }
//    }
}
